import { createRouter, createWebHashHistory, RouteRecordRaw } from "vue-router";
import { usePermissStore } from "../store/permiss";
import Home from "../views/home.vue";

const routes: RouteRecordRaw[] = [
    {
        path: "/",
        redirect: "/dashboard",
    },
    {
        path: "/",
        name: "Home",
        component: Home,
        children: [
            {
                path: "/dashboard",
                name: "dashboard",
                meta: {
                    title: "首页",
                    permiss: "1",
                },
                component: () => import("../views/dashboard.vue"),
            },
            {
                path: "/userlist",
                name: "userlist",
                meta: {
                    title: "用户管理",
                    permiss: "2",
                },
                component: () => import("../views/userlist.vue"),
            },
            {
                path: "/user",
                name: "user",
                meta: {
                    title: "个人中心",
                    permiss: "2",
                },
                component: () => import("../views/user.vue"),
            },
            // banner.vue
            {
                path: "/banner",
                name: "banner",
                meta: {
                    title: "轮播图管理",
                    permiss: "2",
                },
                component: () => import("../views/banner.vue"),
            },
            {
                path: "/bannerlist",
                name: "bannerlist",
                meta: {
                    title: "轮播图列表",
                    permiss: "2",
                },
                component: () => import("../views/bannerlist.vue"),
            },
            {
                path: "/news",
                name: "news",
                meta: {
                    title: "新闻管理",
                    permiss: "2",
                },
                component: () => import("../views/news.vue"),
            },
            {
                path: "/newsList",
                name: "newsList",
                meta: {
                    title: "新闻列表",
                    permiss: "2",
                },
                component: () => import("../views/newsList.vue"),
            },
        ],
    },
    {
        path: "/login",
        name: "Login",
        meta: {
            title: "登录",
        },
        component: () => import("../views/login.vue"),
    },
    {
        path: "/register",
        name: "Register",
        meta: {
            title: "注册",
        },
        component: () => import("../views/register.vue"),
    },
    {
        path: "/403",
        name: "403",
        meta: {
            title: "没有权限",
        },
        component: () => import(/* webpackChunkName: "403" */ "../views/403.vue"),
    },
];

const router = createRouter({
    history: createWebHashHistory(),
    routes,
});

router.beforeEach((to, from, next) => {
    document.title = `${to.meta.title} | 闲暇日报后台`;
    const role = localStorage.getItem("ms_username");
    const permiss = usePermissStore();
    next();
    // if (!role && to.path !== "/login") {
    //     next("/login");
    // } else if (to.meta.permiss && !permiss.key.includes(to.meta.permiss)) {
    //     // 如果没有权限，则进入403
    //     next("/403");
    // } else {
    //     next();
    // }
});

export default router;
